
enum OsnMentionedType: Int {
    case none = 0
    case targets = 1
    case all = 2
    
    init(rawValue: Int) {
        switch rawValue {
        case 0: self = .none
        case 1: self = .targets
        case 2: self = .all
        default: self = .none
        }
    }
}
enum OsnMessageContentType: Int {
    case unknown
    case text
    case card
    case redPacket
    case call
    case file
    case image
    case voice
    case video
    case sticker
    case notify_group
    case notify_add_member
    case notify_kickoff_member
    case notify_create_group
    case notify_delete_group
    case notify_quit_group
    case notify_add_admin
    case notify_kickoff_admin
    case notify_update_group_name
    case notify_mute_group
    
    init(rawValue: Int) {
        switch rawValue {
        case 0: self = .unknown
        case 1: self = .text
        case 2: self = .card
        case 3: self = .redPacket
        case 4: self = .call
        case 5: self = .file
        case 6: self = .image
        case 7: self = .voice
        case 8: self = .video
        case 9: self = .sticker
        case 10: self = .notify_group
        case 11: self = .notify_add_member
        case 12: self = .notify_kickoff_member
        case 13: self = .notify_create_group
        case 14: self = .notify_delete_group
        case 15: self = .notify_quit_group
        case 16: self = .notify_add_admin
        case 17: self = .notify_kickoff_admin
        case 18: self = .notify_update_group_name
        case 19: self = .notify_mute_group
            
            
        default: self = .unknown
        }
    }
    var description: String {
        switch self {
        case .unknown: return "unknown"
        case .text: return "text"
        case .card: return "card"
        case .redPacket: return "redPacket"
        case .call: return "call"
        case .file: return "file"
        case .image: return "image"
        case .voice: return "voice"
        case .video: return "video"
        case .sticker: return "sticker"
        case .notify_group: return "notify_group"
        case .notify_add_member: return "notify_add_member"
        case .notify_kickoff_member: return "notify_kickoff_member"
        case .notify_create_group: return "notify_create_group"
        case .notify_delete_group: return "notify_delete_group"
        case .notify_quit_group: return "notify_quit_group"
        case .notify_add_admin: return "notify_add_admin"
        case .notify_kickoff_admin: return "notify_kickoff_admin"
        case .notify_update_group_name: return "notify_update_group_name"
        case .notify_mute_group: return "notify_mute_group"
        }
    }
}
class OsnNotifyContent: OsnMessageContent {
    static func fromJson(_ c:OsnNotifyContent, _ data:[String:Any]){
        super.fromJson(c, data)
    }
    
    static func notifyGroup(_ notify:OsnNotifyGroup)->String{
        return notify.info
    }
    static func notifyAddMember(_ notify:OsnNotifyAddMember)->String{
        
        var formatMsg = ""
        var count = 0

        // 遍历邀请列表中的成员
        for member in notify.invitees {
            // 获取用户信息
            let userInfo = gosnUser.getUserInfoDefault(member)
            formatMsg += " \(userInfo.getAlias())"
            count += 1
            // 如果已经添加了4个成员，则停止
            if count >= 4 {
                break
            }
        }

        // 如果邀请的成员数量超过已列出的数量
        if notify.invitees.count > count {
            formatMsg += String(format: NSLocalizedString("string_fix_groupCountmember", comment: ""), notify.invitees.count)
        }

        // 添加固定的结束语
        formatMsg += NSLocalizedString("string_fix_joined_group", comment: "")
        return formatMsg
    }
    static func notifyDelMember(_ notify:OsnNotifyDelMember)->String{
        var formatMsg: String

        if gosnUser.myId() == notify.operateUser {
            formatMsg = NSLocalizedString("you", comment: "") + NSLocalizedString("dose", comment: "")
        } else {
            let userInfo = gosnUser.getUserInfoDefault(notify.operateUser)
            formatMsg = userInfo.getAlias() + NSLocalizedString("dose", comment: "")
        }

        var count = 0
        if notify.kickedMembers.contains(gosnUser.myId()) {
            formatMsg += " " + NSLocalizedString("you", comment: "")
            count += 1
        }

        for member in notify.kickedMembers {
            if member == gosnUser.myId() {
                continue
            } else {
                let userInfo = gosnUser.getUserInfoDefault(member)
                formatMsg += " \(userInfo.getAlias())"
                count += 1
                // 如果已经添加了4个成员，则停止
                if count >= 4 {
                    break
                }
            }
        }

        if notify.kickedMembers.count > count {
            formatMsg += String(format: NSLocalizedString("string_fix_groupCountmember", comment: ""), notify.kickedMembers.count)
        }

        formatMsg += " " + NSLocalizedString("remove_member", comment: "")
        return formatMsg
    }
    static func notifyCreateGroup(_ notify:OsnNotifyCreateGroup)->String{
        if notify.creator == gosnUser.myId() {
            return "你创建了" + "\(notify.groupName)"
        }else{
            let user = gosnUser.getUserInfoDefault(notify.creator)
            return "\(user.getAlias())"  +  "创建了" + "\(notify.groupName)"
        }
    }
    static func notifyDeleteGroup(_ notify:OsnNotifyDeleteGroup)->String{
        var formatMsg: String

        if gosnUser.myId() == notify.operateUser {
            formatMsg = NSLocalizedString("you_dismiss_group", comment: "")
        } else {
            let user = gosnUser.getUserInfoDefault(notify.operateUser)
            formatMsg = "\(user.getAlias()) \(NSLocalizedString("dismiss_group", comment: ""))"
        }
        return formatMsg
    }
    static func notifyQuitQroup(_ notify:OsnNotifyQuitGroup)->String{
        var formatMsg: String

        if gosnUser.myId() == notify.quitMember {
            formatMsg = NSLocalizedString("you_quit_group", comment: "")
        } else {
            let user = gosnUser.getUserInfoDefault(notify.quitMember)
            formatMsg = "\(user.getAlias()) \(NSLocalizedString("quit_group", comment: ""))"
        }

        return formatMsg
    }
    static func notifyAddAdmin(_ notify:OsnNotifyAddAdmin)->String{
        var formatMsg = ""
        var count = 0

        // 遍历邀请列表中的成员
        for member in notify.adminMembers {
            // 获取用户信息
            let userInfo = gosnUser.getUserInfoDefault(member)
            formatMsg += " \(userInfo.getAlias())"
            count += 1
            // 如果已经添加了4个成员，则停止
            if count >= 4 {
                break
            }
        }

        // 如果邀请的成员数量超过已列出的数量
        if notify.adminMembers.count > count {
            formatMsg += String(format: NSLocalizedString("string_fix_groupCountmember", comment: ""), notify.adminMembers.count)
        }

        // 添加固定的结束语
        formatMsg += "成为管理员"
        return formatMsg
    }
    static func notifyDelAdmin(_ notify:OsnNotifyDelAdmin)->String{
        var formatMsg = ""
        var count = 0

        // 遍历邀请列表中的成员
        for member in notify.delMembers {
            // 获取用户信息
            let userInfo = gosnUser.getUserInfoDefault(member)
            formatMsg += " \(userInfo.getAlias())"
            count += 1
            // 如果已经添加了4个成员，则停止
            if count >= 4 {
                break
            }
        }

        // 如果邀请的成员数量超过已列出的数量
        if notify.delMembers.count > count {
            formatMsg += String(format: NSLocalizedString("string_fix_groupCountmember", comment: ""), notify.delMembers.count)
        }

        // 添加固定的结束语
        formatMsg += "取消管理员权限"
        return formatMsg
    }
    static func notifyUpdateGroupName(_ notify:OsnNotifyUpdateGroupName)->String{
        return "群名称修改为" + notify.groupName
    }
    
    static func notifyMuteGroup(_ notify:OsnNotifyNotifyMute)->String{
        var formatMsg = ""
        var count = 0
        
        if notify.range == "all" {
            if notify.info == "Mute" {
                return "全员禁言"
            }else {
                return "解除全员禁言"
            }
        } else if notify.range == "you" {
            if notify.info == "Mute" {
                return "你被禁言了"
            }else {
                return "你的禁言已解除"
            }
        } else if notify.range == "members" {
            if notify.info == "Mute" {
                // 遍历邀请列表中的成员
                for member in notify.userList {
                    // 获取用户信息
                    let userInfo = gosnUser.getUserInfoDefault(member)
                    formatMsg += " \(userInfo.getAlias())"
                    count += 1
                }
                
                formatMsg += "已被禁言"
            }else {
                // 遍历邀请列表中的成员
                for member in notify.userList {
                    // 获取用户信息
                    let userInfo = gosnUser.getUserInfoDefault(member)
                    formatMsg += " \(userInfo.getAlias())"
                    count += 1
                }
                
                formatMsg += "禁言已解除"
            }
        }

        return formatMsg
    }
    
    func fromNotify(notify:OsnNotifyContent)->String{
//        guard let notify = content as? OsnNotifyContent else {return ""}
        switch notify.contentType{
        case .notify_group: return OsnNotifyContent.notifyGroup(notify as! OsnNotifyGroup)
        case .notify_add_member: return OsnNotifyContent.notifyAddMember(notify as! OsnNotifyAddMember)
        case .notify_kickoff_member: return OsnNotifyContent.notifyDelMember(notify as! OsnNotifyDelMember)
        case .notify_create_group: return OsnNotifyContent.notifyCreateGroup(notify as! OsnNotifyCreateGroup)
        case .notify_delete_group: return OsnNotifyContent.notifyDeleteGroup(notify as! OsnNotifyDeleteGroup)
        case .notify_quit_group: return OsnNotifyContent.notifyQuitQroup(notify as! OsnNotifyQuitGroup)
        case .notify_add_admin: return OsnNotifyContent.notifyAddAdmin(notify as! OsnNotifyAddAdmin)
        case .notify_kickoff_admin: return OsnNotifyContent.notifyDelAdmin(notify as! OsnNotifyDelAdmin)
        case .notify_update_group_name: return OsnNotifyContent.notifyUpdateGroupName(notify as! OsnNotifyUpdateGroupName)
        case .notify_mute_group: return OsnNotifyContent.notifyMuteGroup(notify as! OsnNotifyNotifyMute)
        default: return ""
        }
    }
}
class OsnMessageContent {
    var mentionedType:OsnMentionedType? = nil
    var mentionedTargets:[String]? = nil
    var contentType = OsnMessageContentType.unknown
    var quoteInfo:OsnQuoteInfo? = nil
    
    func toJson() ->[String:Any] {
        var json:[String:Any] = [:]
        if let a = mentionedType {
            json["mentionedType"] = String(a.rawValue)
        }
        if let a = mentionedTargets {
            json["mentionedTargets"] = a
        }
        if let a = quoteInfo {
            json["quoteInfo"] = a.toJson()
        }
        json["contentType"] = contentType
        return json
    }
    static func fromJson(_ c:OsnMessageContent, _ data:[String:Any]){
        if let a = data["mentionedType"] as? Int {
            c.mentionedType = OsnMentionedType(rawValue: a)
        }
        c.mentionedTargets = data["mentionedTargets"] as? [String]
        //c.contentType
        if let a = data["quoteInfo"] {
            c.quoteInfo = OsnQuoteInfo.fromJson(data)
        }
    }
}
